<?php

namespace backend\models;

use Yii;
use common\models\Common;
use backend\models\Star;
/**
 * This is the model class for table "{{%star_temp}}".
 *
 * @property string $id
 * @property string $name
 * @property string $header
 * @property string $info
 * @property string $desc
 * @property string $url
 * @property string $local_header
 */
class StarTemp extends Common
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%star_temp}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'string', 'max' => 80],
            [['header'], 'string', 'max' => 250],
            [['info'], 'string', 'max' => 200],
            [['desc', 'local_header'], 'string', 'max' => 150],
            [['url'], 'string', 'max' => 100],
            [['new_id','old_id'], 'integer'],
            [['score'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名字',
            'header' => '头像地址',
            'info' => '信息',
            'desc' => '描述',
            'url' => '采集地址',
            'local_header' => '本地地址',
            'new_id'=>'正式ID'
        ];
    }



    /*
     * 处理明星临时数据
     */
    public function dealWithTempStarInfo($tempStarInfo = []){
        $starInfo = [];
        $name = trim($tempStarInfo['name']);

        $name = mb_substr($name,0,-1);
        $name = explode("(",$name);
        if(is_array($name) && count($name) > 1){
            $starInfo['name'] = $name[0];
            $starInfo['nick_name'] = $name[1];
        }else{
            $starInfo['name'] = trim($tempStarInfo['name']);
        }
        $starInfo['desc'] = trim($tempStarInfo['desc']);
        $starInfo['score'] = $tempStarInfo['score'];

        $starInfo = array_merge($starInfo,$this->parseTempInfo(trim($tempStarInfo['info'])));

        $star = new Star();
        $star->setAttributes($starInfo);

        $rs = 0;
        if($star->save()){
            $this->setAttribute('new_id',$star->primaryKey);
            $this->setAttribute('old_id',explode('/',$tempStarInfo['url'])[4]);
            $rs = (int)$this->save();
        }

        return $rs;
    }


    /*
     * 解析标签
     */
    public function parseTempInfo($info=''){
        if(empty($info)) return [];
        $keys = [
            'alias'=>'别 名',
            'age'=>'年 龄',
            'birthday'=>'生 日',
            'star_sign'=>'星 座',
            'birth_place'=>'出 生',
            'height'=>'身 高',
            'weight'=>'体 重',
            'size'=>'三 围',
            'job'=>'职 业',
            'interest'=>'兴 趣',
            'blood_type'=>'血 型',
        ];
        $info = explode(',',$info);
        $infoLen = count($info);

        $keys = array_flip($keys);
        $tags = array_keys($keys);
        $rs = [];
        for ($i = 0;$i < $infoLen;$i++){
            if($i % 2 == 0){
                $key = mb_substr($info[$i],0,-1);

                if(in_array($key,$tags)){
                    $rs[$keys[$key]] = $info[$i+1];
                }
            }
        }

        if(isset($rs['age'])){
            $age = mb_substr($rs['age'],0,-1);
            $age = explode('(',$age);
            $rs['age'] = intval($age[0]);
            $rs['animal'] = trim($age[1]);
            $rs['animal'] = str_replace('属','',$rs['animal']);
        }
        isset($rs['birthday']) && $rs['birthday'] = strtotime($rs['birthday']);
        $starSigns = Yii::$app->params['starSign'];
        $starSigns = array_flip($starSigns);
        isset($rs['star_sign']) && $rs['star_sign'] = $starSigns[trim($rs['star_sign'])];
        isset($rs['weight']) && $rs['weight'] = intval($rs['weight']);
        if(isset($rs['birth_place'])){
            $birth = explode(" ",$rs['birth_place']);
            if(is_array($birth)){
                $rs['country'] = $birth[0];
                $rs['birth_place'] = $birth[1];
            }
        }

        return $rs;
    }
}

